﻿/*****************************************
 * 
 *  copyright (c) by ylshen
 * 
 *  author : ylshen (syl.lucky@gmail.com)
 *  
 *  created at : 08-05-21
 * 
 **************************************** */

using System;
using System.Collections.Generic;
using System.Text;
using JYStockAnalyser.Model;
using JYStockAnalyser.DA;

namespace JYStockAnalyser.Face
{
    /// <summary>
    /// 
    /// </summary>
    public class UserManageFace : System.MarshalByRefObject
    {
        /// <summary>
        /// 获取所有的用户
        /// </summary>
        /// <returns></returns>
        public List<User> GetAllUsers()
        {
            UserMapper mapper = new UserMapper();
            return mapper.FindAllUsers();
        }

        public User AddUser(string userName, string name, string password, string sex, string work, string contractNO, string email, DateTime? endDate)
        {
            return AddUser(userName, name, password, sex, work, contractNO, email, endDate, false);
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="name"></param>
        /// <param name="password"></param>
        /// <param name="sex"></param>
        /// <param name="work"></param>
        /// <param name="contractNO"></param>
        /// <param name="email"></param>
        /// <param name="m_endDate"></param>
        /// <returns></returns>
        public User AddUser(string userName, string name, string password, string sex, string work, string contractNO, string email, DateTime? endDate, bool isGuest)
        {
            User user = new User();
            user.UserName = userName;
            user.Name = name;
            user.Password = password;
            user.Sex = sex;
            user.Work = work;
            user.ContractNO = contractNO;
            user.Email = email;
            user.EndDate = endDate;
            user.IsGuest = isGuest;

            UserMapper mapper = new UserMapper();
            try
            {
                return mapper.InsertUser(user);
            }
            catch (ORException orErr)
            {
                if (orErr.ErrorNumber == ORException.ERROR_NAME_EXISTS)
                {
                    throw new PropertiesExistsException(new string[] { "UserName" });
                }
                throw new CommonException("添加失败");
            }
            catch (Exception)
            {
                throw new CommonException("添加失败");
            }
        }

         public void UpdateUser(int id, string userName, string name, string password, string sex, string work, string contractNO, string email, DateTime? endDate)
         {
             UpdateUser(id, userName, name, password, sex, work, contractNO, email, endDate, false);
         }

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userName"></param>
        /// <param name="name"></param>
        /// <param name="password"></param>
        /// <param name="sex"></param>
        /// <param name="work"></param>
        /// <param name="contractNO"></param>
        /// <param name="email"></param>
        /// <param name="m_endDate"></param>
        public void UpdateUser(int id, string userName, string name, string password, string sex, string work, string contractNO, string email, DateTime? endDate, bool isGuest)
        {
            User user = new User();
            user.Id = id;
            user.UserName = userName;
            user.Name = name;
            user.Password = password;
            user.Sex = sex;
            user.Work = work;
            user.ContractNO = contractNO;
            user.Email = email;
            user.EndDate = endDate;
            user.IsGuest = isGuest;

            UserMapper mapper = new UserMapper();
            try
            {
                mapper.UpdateUser(user);
            }
            catch (ORException orErr)
            {
                if (orErr.ErrorNumber == ORException.ERROR_NAME_EXISTS)
                {
                    throw new PropertiesExistsException(new string[] { "UserName" });
                }
                throw new CommonException("更新失败");
            }
            catch (Exception)
            {
                throw new CommonException("更新失败");
            }
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        public void DeleteUser(int id)
        {
            new UserMapper().DeleteUser(id);
        }

        /// <summary>
        /// 清空用户
        /// </summary>
        public void ClearUsers()
        {
            new UserMapper().ClearUsers();
        }

       
        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="user"></param>
        /// <returns>1:成功 , 2:没有此用户 , 3:密码错误 , , 5:已过期 , 6首次登录</returns>
        public byte Login(string userName, string password, out User user)
        {
            UserMapper mapper = new UserMapper();
            user = mapper.FindUserByUserName(userName);
            if (user == null)
            {
                return 2;
            }

            if (!user.Password.Equals(password))
            {
                return 3;
            }

            if (user.EndDate.HasValue && DateTime.Compare(user.EndDate.Value, DateTime.Now) == -1)
            {
                return 5;
            }

            if (user.UniqueNO == null)
            {
                return 6;
            }

            return 1;
        }

        /// <summary>
        /// 将指定用户与指定计算机绑定
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="uniqueNO"></param>
        /// <returns>1:成功 , 110:失败</returns>
        public byte BindComputer(string userName, ComputerUniqueNO uniqueNO)
        {
            UserMapper mapper = new UserMapper();
            User user = mapper.FindUserByUserName(userName);

            user.UniqueNO = uniqueNO;

            try
            {
                mapper.UpdateUser(user);
                return 1;
            }
            catch (System.Exception)
            {
                return 110;
            }
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public byte ChangePassword(string userName, string password)
        {
            UserMapper mapper = new UserMapper();
            User user = mapper.FindUserByUserName(userName);

            user.Password = password;

            try
            {
                mapper.UpdateUser(user);
                return 1;
            }
            catch (System.Exception)
            {
                return 110;
            }
        }
    }
}
